package com.shanghaionstar.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.weight.CustomImageView;
import com.onstar.cn.weight.DeleteEditText;
import com.shanghaionstar.R;

/**
 * Created by 7828wc on 16/7/1.
 */
public class CommonInputLayout extends LinearLayout {
    private View view;
    private CustomImageView left_icon, right_icon, line_layout;
    private DeleteEditText edit_value;
    private TextView tv_preffix,edit_error_text, edit_hint_text;
    private Button mRightBtn;
    private ProgressBar request_loading;
    private boolean isCheck = false;

    public CommonInputLayout(Context context) {
        super(context);
        initView(null);
    }

    public CommonInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public CommonInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        view = inflate(getContext(), R.layout.onstar_edittext_layout, this);
        edit_error_text = (TextView) view.findViewById(R.id.edit_error_text);
        edit_hint_text = (TextView) view.findViewById(R.id.edit_hint_text);
        tv_preffix = (TextView) view.findViewById(R.id.tv_preffix);
        left_icon = (CustomImageView) view.findViewById(R.id.left_icon);
        right_icon = (CustomImageView) view.findViewById(R.id.right_icon);
        line_layout = (CustomImageView) view.findViewById(R.id.line_layout);
        edit_value = (DeleteEditText) view.findViewById(R.id.edit_value);
        mRightBtn = (Button) view.findViewById(R.id.reset_btn);
        request_loading = (ProgressBar) view.findViewById(R.id.request_loading);
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, com.onstar.cn.weight.R.styleable.common_input);
            int rightIcon = typedArray.getResourceId(com.onstar.cn.weight.R.styleable.common_input_rightIcon, -1);
            int leftIcon = typedArray.getResourceId(com.onstar.cn.weight.R.styleable.common_input_leftIcon, -1);
            String hintText = typedArray.getString(com.onstar.cn.weight.R.styleable.common_input_hintText);
            String checkHintText = typedArray.getString(com.onstar.cn.weight.R.styleable.common_input_checkHintText);
            boolean isClearAll = typedArray.getBoolean(com.onstar.cn.weight.R.styleable.common_input_clearAll, true);
            boolean isShowLine = typedArray.getBoolean(com.onstar.cn.weight.R.styleable.common_input_isShowLine, true);
            boolean isShowClearBtn = typedArray.getBoolean(com.onstar.cn.weight.R.styleable.common_input_showClearBtn, true);
            String tvPrefix = typedArray.getString(com.onstar.cn.weight.R.styleable.common_input_tvPreffix);

            int inputType = typedArray.getInt(com.onstar.cn.weight.R.styleable.common_input_inputType, 1);
            edit_value.setInputType(inputType);

            edit_value.setClearAll(isClearAll);
            edit_value.setShowClearBtn(isShowClearBtn);

            if (rightIcon != -1) {
                right_icon.setImageResource(rightIcon);
                right_icon.setVisibility(View.VISIBLE);
            } else {
                right_icon.setVisibility(View.GONE);
            }

            if (leftIcon != -1) {
                left_icon.setImageResource(leftIcon);
                left_icon.setVisibility(View.VISIBLE);
            } else {
                left_icon.setVisibility(View.GONE);
            }

            if(!CommonUtil.isNull(tvPrefix)) {
                tv_preffix.setText(tvPrefix);
            }
            line_layout.setVisibility(isShowLine ? View.VISIBLE : View.GONE);

            edit_value.setHint(hintText);
            edit_value.setVisibility(CommonUtil.isNull(hintText) ? View.GONE : View.VISIBLE);

            edit_hint_text.setVisibility(CommonUtil.isNull(checkHintText) ? View.GONE : View.VISIBLE);
            edit_hint_text.setText(checkHintText);
            typedArray.recycle();
        }
    }

    public DeleteEditText getEditValue() {
        return edit_value;
    }

    public TextView getTv_preffix(){
        return  tv_preffix;
    }

    public String getText() {
        return edit_value.getText().toString();
    }

    public void setLeftIcon(int resId) {
        if (resId != -1) {
            left_icon.setImageResource(resId);
            left_icon.setVisibility(View.VISIBLE);
        } else {
            left_icon.setVisibility(View.GONE);
        }
    }

    public void showLoading(boolean isShow) {
        if (isShow) {
            request_loading.setVisibility(View.VISIBLE);
            right_icon.setVisibility(View.GONE);
        } else {
            if (request_loading.getVisibility() == View.VISIBLE) {
                request_loading.setVisibility(View.GONE);
            }
            right_icon.setVisibility(right_icon.getVisibility() == View.VISIBLE ? View.VISIBLE : View.GONE);
        }
    }

    public void setRightIcon(int resId) {
        if (resId != -1) {
            right_icon.setImageResource(resId);
            right_icon.setVisibility(View.VISIBLE);
            request_loading.setVisibility(View.GONE);
        } else {
            request_loading.setVisibility(View.GONE);
            right_icon.setVisibility(View.GONE);
        }
    }

    public void setRightIcon(byte[] imageData) {
        if (imageData.length != 0) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
            right_icon.setImageBitmap(bitmap);

            if (bitmap != null && bitmap.isRecycled()) {
                bitmap.recycle();
            }
            request_loading.setVisibility(View.GONE);
            right_icon.setVisibility(View.VISIBLE);
        } else {
            request_loading.setVisibility(View.GONE);
            right_icon.setVisibility(View.GONE);
        }
    }

    public void setRightIconClickListener(OnClickListener onClickListener) {
        if (right_icon != null && right_icon.getVisibility() == View.VISIBLE)
            right_icon.setOnClickListener(onClickListener);
    }

    public void setRightBtnVisible(boolean isShow){
        mRightBtn.setVisibility(isShow ? VISIBLE : GONE);
    }

    public void setRightBtnClickListener(OnClickListener onClickListener) {
        if(mRightBtn.getVisibility() == View.GONE)
            mRightBtn.setVisibility(VISIBLE);
        if (mRightBtn != null && mRightBtn.getVisibility() == View.VISIBLE)
            mRightBtn.setOnClickListener(onClickListener);
    }

    public void setCheckHintText(String checkHintText) {
        isCheck = CommonUtil.isNull(checkHintText);
        edit_error_text.setVisibility(CommonUtil.isNull(checkHintText) ? View.GONE : View.VISIBLE);
        edit_error_text.setText(checkHintText);
    }

    public void setCheckHintText(int resId) {
        setCheckHintText(getContext().getString(resId));
    }

    public void setHintText(String hintText) {
        edit_hint_text.setVisibility(CommonUtil.isNull(hintText) ? View.GONE : View.VISIBLE);
        edit_hint_text.setText(hintText);
    }

    public void setHintText(int resId) {
        setHintText(getContext().getString(resId));
    }

    public void setEditTextHint(int resId) {
        edit_value.setHint(getContext().getString(resId));
    }

    public void setEditTextHint(String value) {
        edit_value.setHint(value);
    }

    public void isShowPassword(boolean isShow) {
        if (isShow) {
            // 显示明文
            edit_value.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            // 显示密文
            edit_value.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }

    public void setOnTextChangeListener(DeleteEditText.OnTextChangeListener onTextChangeListener) {
        edit_value.setOnTextChangeListener(onTextChangeListener);
    }

    public void setOnFocusChangeListener(View.OnFocusChangeListener onFocusChangeListener) {
        edit_value.setOnFocusChangeListener(onFocusChangeListener);
    }

    public void setInputType(int type) {
        edit_value.setInputType(type);
    }
    public void setText(int resID) {
        edit_value.setText(resID);
    }

    public void setText(String text) {
        edit_value.setText(text);
    }

    public void setEditEnabled(boolean enabled) {
        edit_value.setEnabled(enabled);
    }

    public void setCheckOkBg(int resId) {
        edit_value.setCheckOkBg(resId);
    }

    public void recyle() {
        edit_value.onDestory();
    }

    public void clearAll() {
        edit_value.clearAll();
    }

    public boolean isCheck() {
        return isCheck;
    }

    public void setCheck(boolean check) {
        isCheck = check;
    }
}
